package cn.tedu.test.luban.rpc.server;

import cn.tedu.test.luban.rpc.api.HelloService;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {

    public static void main(String[] args) throws IOException {

        ServerSocket serverSocket = new ServerSocket(20000);

        while(true){
            System.out.println("等待客户端连接...");
            Socket socket = serverSocket.accept();
            process(socket); //解析数据
            System.out.println("服务端接收到客户端socket");
        }

    }

    private static void process(Socket socket) throws IOException {
        InputStream inputStream = socket.getInputStream();
        DataInputStream dis = new DataInputStream(inputStream);
        String name = dis.readUTF();

        System.out.println("服务端反序列化数据:"+name);

        //调用服务,将结果返回给客户端
        HelloService helloService=new HelloServiceImpl();
        String result = helloService.sayHi(name);

        OutputStream outputStream = socket.getOutputStream();
        DataOutputStream dos = new DataOutputStream(outputStream);
        dos.writeUTF(result);

    }

}
